/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package com.stip.server.da;

import com.stip.server.entities.Admin;
import com.stip.server.entities.Organization;
import com.stip.server.entities.UsersMain;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author BlueBerry
 */
@Stateless
@LocalBean
public class AdminDA {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @PersistenceContext
    EntityManager em;
    public Admin findAdmin(Integer id){
    return  em.find(Admin.class, id);
    }
    public Integer addAdmin(Integer orgID,String UID){
        if (em.find(Organization.class, orgID)!=null&&em.find(UsersMain.class, UID)!=null) {
            Admin admin=new Admin();
            admin.setOrgId(em.find(Organization.class, orgID));
            admin.setUid(em.find(UsersMain.class, UID));
            em.persist(admin);
            em.flush();
            return  admin.getId();
        } 
        return  0;
    }
        public Boolean deleteAdmin(Integer id){
            if (em.find(Admin.class, id)!=null) {
                em.remove(em.find(Admin.class, id));
                return  true;
            } else {
                return  false;
            }
        
        }
}
